<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Crypt;
use DB;
class UserController extends Controller
{
    //
     public function add()
    {
        return view('admin.user.add',['title' => '用户添加']);
    }
    public function insert(Request $request)
    {
        $this->validate($request, [
           'email' => 'required|min:6|max:18',
           'password' => 'required|min:6|max:18',
             'repassword' => 'required|same:password',
         ], [
             'email.required' => '用户邮箱没有填写',
             'email.min' => '用户名小于6位',
             'email.max' => '用户名大于18位',
             'password.required' => '请填写密码',
             'password.min' => '密码小于6位',
             'password.max' => '密码大于18位',
             'repassword.required' => '请确认密码',
             'repassword.same' => '两次密码输入不正确',
         ]);
        //return view ('admin.user.insert');

        //$data = $request -> except('_token','repassword');

        $data = $request -> only('email','password','pic','status');

        // //处理密码字段
         $data['password'] = Crypt::encrypt($data['password']);
        // //处理status字段
         $data['status'] = 0;

        //处理图片
         if($request -> hasFile('pic'))
         {
            if($request -> file('pic') -> isValid())
            {
                $suffix = $request -> file('pic') -> getClientOriginalExtension();
                $fileName = time().mt_rand(10000,99999).'.'.$suffix;

                $move = $request -> file('pic') -> move('./uploads/yhtx',$fileName);

                if($move)
                {
                    $data['pic'] = $fileName;
                }
                else
                {
                    $data['pic'] = 'default.jpg';
                }
            }
            else
                {
                    $data['pic'] = 'default.jpg';
                }
         }
         else
                {
                    $data['pic'] = 'default.jpg';
                }

                // dd($data);

                $res = DB::table('user') -> insert($data);


                if($res)
                {
                    return redirect('admin/user/index') -> with(['title' => '用户列表']);
                }else
                {
                    return back();
                }

    }
    //列表页
    public function index(Request $request)
    {
        $data = DB::table('user')  -> where('email','like','%'.$request -> input('keywords').'%') -> paginate($request -> input('num',2));

        // echo 'index';
        return view('admin.user.index', ['data' => $data,'request' => $request -> all()]);
    }
    //修改用户状态
    public function ajaxStatus(Request $request)
    {

         $id = $request -> input('id');
         $status = DB::table('user') -> where('id',$id) -> first() -> status;

         if($status == 0)
         {
            $res = DB::table('user') -> where('id',$id) -> update(['status' => 1]);
            if($res)
            {
                return 1;
            }else
            {
                return 2;
            }
         }
         else if($status == 1)
        {
            $res = DB::table('user') -> where('id',$id) -> update(['status' => 0]);
             if($res)
            {
                return 0;
            }else
            {
                return 2;
            }
        }
    }
    //edit
    public function edit($id)
    {
        $data = DB::table('user') -> where('id',$id) -> first();
        return view('admin.user.edit',['data' => $data,'title' => '编辑页面']);
    }
    //update
    public function update(Request $request)
    {
        $data = $request -> except('_token','id');
        //获取原来的老图片
          $oldPic = DB::table('user') -> where('id',$request -> id) -> first() -> pic;
        if($request -> hasFile('pic'))
        {
             if($request -> file('pic') -> isValid())
             {
                 $suffix = $request -> file('pic') -> getClientOriginalExtension();
               $fileName = time().mt_rand(10000,99999).'.'.$suffix;
            //移动文件
            $move = $request -> file('pic') ->move('./uploads/yhtx',$fileName);
             if($move)
            {
              $data['pic'] = $fileName;
              //上传成功 后删掉原来的图片
              //unlink('./uploads/yhtx/'.$oldPic);
            }
             }
        }
        //执行修改
         $res = DB::table('user') -> where('id',$request -> id) -> update($data);
            if($res)
            {
                return redirect('/admin/user/index');
            }else
            {
                return back();
            }
    }
    //删除
    public function delete($id)
    {
        $res = DB::table('user') -> delete($id);
         if($res)
            {
                return redirect('/admin/user/index');
            }else
            {
                return back();
            }
    }
}

